/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package org.myontology.view.items;

import java.util.ArrayList;
import java.util.List;

import model.Vocabulary;

import org.apache.commons.lang.StringEscapeUtils;
import org.myontology.model.LabelWrapper;
import org.myontology.model.Property;
import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;

import controller.elementMapper.Mapper;


public class AbstractComponent extends _AbstractComponent {
	

	private static final String CSSIDFIELD = "div";
	public static final String CSSIDCONTENT = "divcontent";
	public static final String NAMEFORMELEMENT = "Entry";
	
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public String getFieldInViewMode( Property p, boolean showNavigation, boolean showNavigationInEditMode, String lang ) throws Exception {
		String ret = "";

		ret += getTitleContainer( new LabelWrapper(p,null), true, false );
		
		String content = getFormInViewMode(p,  showNavigation, showNavigationInEditMode, lang );
		
		ret += getFormContainer( content, CSSIDCONTENT+p.getKey() );

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD+p.getKey() );
		
		return ret;
	}

	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public String getFieldInEditMode( Property p, String entry, ArrayList<Exception> exceptions, boolean showNavigation, String lang ) throws Exception {
		
		String ret = "";
					
		
		ret += getTitleContainer( new LabelWrapper(p,null), true, false );

		String content = getFormInEditMode(p,  entry, exceptions, showNavigation, lang );

		ret += getFormContainer( content, CSSIDCONTENT+p.getKey() );

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD+p.getKey() );
		
		return ret;
	}
	
	
	
	
	
	
	
	public String getFormInViewMode( Property p, boolean showNavigation, boolean showNavigationInEditMode, String lang ) throws Exception {
		
		URI uri = new URIImpl(p.getNamespace()+p.getValueString());
		
		String ret = "";

		String content = "";	

		String entry = p.getValueString();
		if( entry==null ) {
			content = NOTHINGSELECTED;
		}
		else {
			content = getHTMLContent( entry );		
		}
		/*
		ret = renderViewForm( 
				content, 
				entry,
				CSSIDCONTENT, 
				showNavigation );
				*/
		ret = renderViewForm(
				p,
				content, 
				Mapper.selectHasVersionDateTimeString(uri),
				CSSIDCONTENT+p.getKey(), 
				showNavigation, 
				showNavigationInEditMode,false,"generic" );
				
		return ret;
	}
	
	private String getHTMLContent( String entry ) {
		
		String ret = "";
		
		
		// the number of characters shown collapsed.
		final int SIZE = 350;

		String longentry = StringEscapeUtils.escapeHtml( entry ).replace("\n", "<br />").replace("\r", "" );
		
		if( entry.length() < SIZE + 15 ) { // 15 is tolerance so that the zoom-button is not shown only for small amount of chars.
			ret += longentry;
		}
		else {

			String shortentry = StringEscapeUtils.escapeHtml( entry.substring(0, SIZE) ).replace("\n", "<br />").replace("\r", "" ) + "...";
		
			String linkToShort = " <img src='skins/default/images/zoom_out.png' alt='shorten' title='shorten' />";
			String linkToLong = " <img src='skins/default/images/zoom_in.png' alt='show all' title='show all' />";
		
			ret += 
		"<span id=\"extendableComment\">\n"+
		"	<span id=\"commentContainerSwitchable\">"+ shortentry +"</span>\n"+
		"	<span style=\"cursor:pointer\" id=\"commentSwitch\" onclick=\"javascript:switchComment('" + StringEscapeUtils.escapeJavaScript( longentry ) +  "','" + StringEscapeUtils.escapeJavaScript( shortentry ) +  "','" + StringEscapeUtils.escapeJavaScript( linkToShort ) + "','" + StringEscapeUtils.escapeJavaScript( linkToLong ) + "');\">" + linkToLong + "</span>\n"+
		"</span>\n";
		
			
		}

		ret = "<div id=\"commentContainer\">" + ret + "</div>\n";
		
		return ret;
	}
	

	public String getFormInEditMode( Property p, String entry, List<Exception> exceptions, boolean showNavigation, String lang ) throws Exception {
		URI uri = new URIImpl(p.getNamespace()+p.getValueString());
		String ret = "";


		String actualVersionContent = NOTHINGSELECTED;
		String actualTimestamp = Mapper.selectHasVersionDateTimeString(uri);

		String label = "";	
		if( uri!=null ) {
			String tmp = p.getValueString();
			if( tmp!=null ) {
				actualVersionContent = getHTMLContent( tmp );				
			}
			

			label = Mapper.selectLabel( uri, Vocabulary.ENGLISH);
			if( label==null )
				label = "";
		}

		int rows = 4;
		int cols = 60;		
		if(entry==null)
			entry="";
		else {
			rows = entry.split("\n").length;
			
			rows += entry.length() / cols;
			
			rows++;
			
			if( rows < 4 )
				rows = 4;
			
			entry += " ";
		}
		
		String content = "<textarea tabindex=\"\" id=\""+NAMEFORMELEMENT+p.getKey()+"\" name=\""+NAMEFORMELEMENT+p.getKey()+"\" rows=\"" + rows + "\" cols=\"" + cols + "\">" + entry + "</textarea>";

		content +=
			"<div class=\"inputhint\">Free text; do not use any hypertext like HTML!</div>";

		


		ret = renderEditForm( 
				p, 
				content,
				exceptions, 
				actualVersionContent,
				actualTimestamp, // actualversionContent is the same as its entry; not the same e.g. at Tags because there actualverionContent would be linked 
				CSSIDCONTENT+p.getKey(), 
				NAMEFORMELEMENT+p.getKey(), 
				showNavigation );
		
		return ret;
	}
	

	
	

}
